package com.yf.tz.service.impl;

import com.yf.response.RRException;
import com.yf.tz.dao.MallSkuDao;
import com.yf.tz.entity.MallSkuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.yf.tz.dao.MallGoodsDao;
import com.yf.tz.entity.MallGoodsEntity;
import com.yf.tz.service.MallGoodsService;
import com.yf.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Jul 01 16:32:18 CST 2020
 */
@Service
public class MallGoodsServiceImpl extends BaseServiceImpl<MallGoodsEntity> implements MallGoodsService {

    private MallGoodsDao mallGoodsDao;


    @Autowired
    public void setDao(MallGoodsDao mallGoodsDao) {
        this.mallGoodsDao = mallGoodsDao;
        super.setBaseDao(mallGoodsDao);
    }


    /**
     *
     *
     @Override
     List<MallGoodsVo> queryListVo(MallGoodsVo mallGoodsVo){
            return mallGoodsDao.queryListVo(mallGoodsVo);
     }

     @Override
     MallGoodsVo getVo(Long id){
                return mallGoodsDao.getVo(id);
    }

     **/


    /**内部方法**/



    /**外部方法实现**/

    @Override
    public List<MallGoodsEntity> findTop() {
        return mallGoodsDao.findTop();
    }


    @Override
    public List<MallGoodsEntity> findByType(Long typeId) {
        return mallGoodsDao.findByTypeId(typeId);
    }

    @Override
    public List<MallGoodsEntity> findByTypeAndName(Long typeId, String name) {
        return mallGoodsDao.findByTypeAndName(typeId,name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void decreaseStock(Long goodsId, Long skuId, Integer number) {
        MallGoodsEntity goodsEntity = get(goodsId);
        if(goodsEntity.getStock()<number){
            number = goodsEntity.getStock();
//            throw new RRException(goodsEntity.getName()+"库存不足!");
        }
        mallGoodsDao.decreaseStock(goodsId,number);
        if(skuId==null)return;
        MallSkuEntity skuEntity = skuDao.get(skuId);
        if(skuEntity.getStock()<number){
            number = skuEntity.getStock();
//            throw new RRException(goodsEntity.getName()+skuEntity.getName()+"库存不足");
        }
        skuDao.decreaseStock(skuId,number);
    }

    @Override
    public void validateStock(Long goodsId, Long skuId, Integer number) {
        MallGoodsEntity goodsEntity = get(goodsId);
        if(goodsEntity.getStock()<number){
            throw new RRException(goodsEntity.getName()+"库存不足!");
        }
        if(skuId==null)return;
        MallSkuEntity skuEntity = skuDao.get(skuId);
        if(skuEntity.getStock()<number){
            throw new RRException(goodsEntity.getName()+skuEntity.getName()+"库存不足");
        }
    }

    @Override
    public void increaseStock(Long goodsId, Long skuId, Integer number) {
        mallGoodsDao.increaseStock(goodsId,number);
        if(skuId==null)return;
        skuDao.increaseStock(skuId,number);
    }

    @Autowired
    private MallSkuDao skuDao;
}
